/**
 * arr  黄图背景的数组
 * arr1 奖牌上同步转盘的礼品数组
 * arr2 奖牌旁边同步转盘所动的文本
 * arr3 转盘上的礼品的数组
 */

class Luck_drawUI extends egret.Sprite {
    private ding;
    private win;
    private defeated;

    public constructor() {
        super();
        this.once(egret.Event.ADDED_TO_STAGE, this.createView, this);
    }
    private createView(): void {
        // //添加背景
        // var bg = drawReactShape(null, 0, 0, 640, 1136, 0x16c5ee);
        // this.addChild(bg);

        // var bg_shuiwen = createBitmap("bg_shuiwen_png", -560, 0);
        // this.addChild(bg_shuiwen);
        // var bg_shuiwen2 = createBitmap("bg_shuiwen_png", -560, -1136);
        // this.addChild(bg_shuiwen2);
        // egret.Tween.get(bg_shuiwen, { loop: true }).to({ x: 0, y: 1136 }, 10000);
        // egret.Tween.get(bg_shuiwen2, { loop: true }).to({ x: 0, y: 0 }, 10000);
        // //添加音乐logo
        // // var circular = createBitmap("circular_png",546,16);
        // // this.addChild(circular);
        // var music_logo = createBitmap("music_logo_png", 580, 50);
        // this.addChild(music_logo);
        // music_logo.anchorOffsetX = music_logo.width * .5;
        // music_logo.anchorOffsetY = music_logo.height * .5;
        // music_logo.touchEnabled = true;
        // egret.Tween.get(music_logo, { loop: true }).to({ rotation: 360 }, 3000);
        //声音
        this.ding = new egret.Sound;    //叮一声，转盘移动一格响一次
        this.ding.load("resource/assets/ding.mp3");
        this.win = new egret.Sound;     //中奖时响一声
        this.win.load("resource/assets/cheer.mp3")
        this.defeated = new egret.Sound;//没中奖响一声
        this.defeated.load("resource/assets/defeated.mp3")
        // //添加音乐
        // var sound = new egret.Sound;
        // var soundChannel;
        // var totalLength = 0;
        // var curTime = 0;
        // sound.addEventListener(egret.Event.COMPLETE, function () {
        //     soundChannel = sound.play(curTime, 1);
        //     totalLength = sound.length;
        // }, this);
        // sound.load("resource/assets/let_it_go.mp3");
        // //通过点击音乐logo播放/暂停音乐
        // music_logo.addEventListener(egret.TouchEvent.TOUCH_TAP, function () {
        //     if (soundChannel == null) {
        //         soundChannel = sound.play(curTime);
        //         egret.Tween.resumeTweens(music_logo);
        //     } else {
        //         curTime = soundChannel.position;
        //         soundChannel.stop();
        //         soundChannel = null;
        //         egret.Tween.pauseTweens(music_logo);
        //     }
        // }, this)


        //奖牌
        var medal = createBitmap("prize_medal_png", 42, 0);
        this.addChild(medal);

        //九宫格背景
        var jiugongge = createBitmap("jiugongge_png", 42, 373);
        this.addChild(jiugongge);
        //点击抽奖的三个按钮（在同一个位置
        var cj_btn=my_button("点击\n抽奖", 55, 0xff4c22, true, 180, 180, 230, 563, "luck_cj_button_jpg", 9, 0xfff530);
        this.addChild(cj_btn);
        cj_btn.touchEnabled=true;
        this.setChildIndex(cj_btn,2);
        // var zaicichoujiang = createBitmap("zaicichoujiang_jpg", 230, 563);
        // this.addChild(zaicichoujiang);
        // var choujiangzhong = createBitmap("choujiangzhong_jpg", 230, 563);
        // this.addChild(choujiangzhong);
        // var dianjichoujiang = createBitmap("dianjichoujiang_jpg", 230, 563);
        // this.addChild(dianjichoujiang);
        // dianjichoujiang.touchEnabled = true;
        // zaicichoujiang.touchEnabled = true;
        // dianjichoujiang.addEventListener(egret.TouchEvent.TOUCH_TAP, function () {
        //     egret.Tween.get(huangtu_1).to({ x: 223 }, 0).to({ x: 223 }, 1000).to({ x: 412 }, 0).to({ x: 412 }, 1000).to({ y: 542 }, 0).to({ y: 542 }, 1000).to({ y: 732 }, 0)
        // }, this);

        //轮播黄图所停留格的所在的位置        
        var huangtu_1 = createBitmap("tingliuge_jpg", 30, 343);
        this.addChild(huangtu_1);
        huangtu_1.visible = false;
        var huangtu_2 = createBitmap("tingliuge_jpg", 209, 343);
        this.addChild(huangtu_2);
        huangtu_2.visible = false;
        var huangtu_3 = createBitmap("tingliuge_jpg", 392, 343);
        this.addChild(huangtu_3);
        huangtu_3.visible = false;
        var huangtu_4 = createBitmap("tingliuge_jpg", 30, 542);
        this.addChild(huangtu_4);
        huangtu_4.visible = false;
        var huangtu_6 = createBitmap("tingliuge_jpg", 392, 542);
        this.addChild(huangtu_6);
        huangtu_6.visible = false;
        var huangtu_7 = createBitmap("tingliuge_jpg", 30, 742);
        this.addChild(huangtu_7);
        huangtu_7.visible = false;
        var huangtu_8 = createBitmap("tingliuge_jpg", 209, 742);
        this.addChild(huangtu_8);
        huangtu_8.visible = false;
        var huangtu_9 = createBitmap("tingliuge_jpg", 392, 742);
        this.addChild(huangtu_9);
        huangtu_9.visible = false;
        var arr = [
            huangtu_1, huangtu_2, huangtu_3, huangtu_6, huangtu_9, huangtu_8, huangtu_7, huangtu_4
        ]
        for (var i = 0; i < arr.length; i++) {
            arr[i].x += arr[i].width * .5;
            arr[i].y += arr[i].height * .5;
            arr[i].anchorOffsetX = arr[i].width * .5;
            arr[i].anchorOffsetY = arr[i].height * .5;

        }
        // arr[rannum_reday].visible = true;
        //奖牌上同步转盘所动的礼品
        var medal_1 = createBitmap("prize_01_png", 100, 150);
        this.addChild(medal_1);
        var medal_2 = createBitmap("prize_02_png", 100, 150);
        this.addChild(medal_2);
        var medal_3 = createBitmap("prize_03_png", 100, 150);
        this.addChild(medal_3);
        var medal_4 = createBitmap("prize_04_png", 70, 180);
        this.addChild(medal_4);
        var medal_6 = createBitmap("prize_06_png", 120, 175);
        this.addChild(medal_6);
        var medal_7 = createBitmap("prize_07_png", 100, 150);
        this.addChild(medal_7);
        var medal_8 = createBitmap("prize_08_png", 70, 170);
        this.addChild(medal_8);
        var medal_9 = createBitmap("prize_09_png", 100, 150);
        this.addChild(medal_9);
        var medal_10 = createBitmap("prize_wenhao_png", 140, 170);
        this.addChild(medal_10);
        var arr1 = [medal_1, medal_2, medal_3, medal_6, medal_9, medal_8, medal_7, medal_4]
        for (var i = 0; i < arr1.length; i++) {              //遍历奖牌内的礼品数组
            arr1[i].visible = false;
            // arr1[rannum_reday].visible = true;
        };
        //奖牌旁边同步转盘所动的文本
        var text_1 = createTextFiled("奥利帝克户外背包", 300, 200, 35, 0xffffff, "center", 330, 60, "middle");
        this.addChild(text_1);
        var text_2 = createTextFiled("迪卡侬透气T恤", 300, 200, 35, 0xffffff, "center", 330, 60, "middle");
        this.addChild(text_2);
        var text_3 = createTextFiled("乐扣运动水壶", 300, 200, 35, 0xffffff, "center", 330, 60, "middle");
        this.addChild(text_3);
        var text_6 = createTextFiled("谢谢参与", 300, 200, 35, 0xffffff, "center", 330, 60, "middle");
        this.addChild(text_6);
        var text_9 = createTextFiled("迪卡侬运动毛巾", 300, 200, 35, 0xffffff, "center", 330, 60, "middle");
        this.addChild(text_9);
        var text_8 = createTextFiled("多功能运动腰包", 300, 200, 35, 0xffffff, "center", 330, 60, "middle");
        this.addChild(text_8);
        var text_7 = createTextFiled("安联运动意外\n保险计划(7天)", 300, 200, 35, 0xffffff, "center", 330, 80, "middle");
        this.addChild(text_7);
        var text_4 = createTextFiled("小米运动手环", 300, 200, 35, 0xffffff, "center", 330, 60, "middle");
        this.addChild(text_4);
        var text_10 = createTextFiled("恭喜你!\n你的奖品为\n", 300, 130, 35, 0xffffff, "center", 330, 80, "middle");
        this.addChild(text_10);
        var text_11 = createTextFiled("? ? ? ? ? ?", 300, 200, 35, 0xffffff, "center", 330, 60, "middle");
        this.addChild(text_11);
        var arr2 = [text_1, text_2, text_3, text_6, text_9, text_8, text_7, text_4]
        text_10.visible = false;
        text_10.fontFamily = "DFKai-SB";
        for (var i = 0; i < arr2.length; i++) {
            arr2[i].visible = false;
            // arr2[rannum_reday].visible = true;
            arr2[i].bold = true;
            arr2[i].fontFamily = "Microsoft JhengHei";
        }
        //九宫格上面的礼物
        var prize_1 = createBitmap("prize_01_png", 70, 355);
        this.addChild(prize_1);
        var prize_2 = createBitmap("prize_02_png", 245, 355);
        this.addChild(prize_2);
        var prize_3 = createBitmap("prize_03_png", 440, 360);
        this.addChild(prize_3);
        var prize_4 = createBitmap("prize_04_png", 40, 600);
        this.addChild(prize_4);
        var prize_6 = createBitmap("prize_06_png", 450, 605);
        this.addChild(prize_6);
        var prize_7 = createBitmap("prize_07_png", 60, 750);
        this.addChild(prize_7);
        var prize_8 = createBitmap("prize_08_png", 240, 780);
        this.addChild(prize_8);
        var prize_9 = createBitmap("prize_09_png", 440, 760);
        this.addChild(prize_9);
        //九宫格上面的礼物的文本
        var prize_t1 = createTextFiled("奥利帝克户外背包", 68, 510, 18, 0x000000, "center", 150, 30, "middle");
        this.addChild(prize_t1);
        var prize_t2 = createTextFiled("迪卡侬透气T恤", 258, 510, 18, 0x000000, "center", 150, 30, "middle");
        this.addChild(prize_t2);
        var prize_t3 = createTextFiled("乐扣运动水壶", 448, 510, 18, 0x000000, "center", 150, 30, "middle");
        this.addChild(prize_t3);
        var prize_t9 = createTextFiled("迪卡侬运动毛巾", 448, 890, 18, 0x000000, "center", 150, 30, "middle");
        this.addChild(prize_t9);
        var prize_t8 = createTextFiled("多功能运动腰包", 258, 890, 18, 0x000000, "center", 150, 30, "middle");
        this.addChild(prize_t8);
        var prize_t7 = createTextFiled("安联运动意外\n保障计划(7天)", 68, 870, 18, 0x000000, "center", 150, 60, "middle");
        this.addChild(prize_t7);
        var prize_t4 = createTextFiled("小米运动手环", 68, 695, 18, 0x000000, "center", 150, 30, "middle");
        this.addChild(prize_t4);
        var arr3 = [prize_1, prize_2, prize_3, prize_6, prize_9, prize_8, prize_7, prize_4]


        //添加分享遮罩层
        var fenxiang_mask = drawReactShape(null, 0, 0, 640, 1136, 0x000000);
        fenxiang_mask.alpha = 0.8;
        var fenxinag = createBitmap("fenxiang_png",178,39);
        var rongqi1 = new egret.DisplayObjectContainer;
        this.addChild(rongqi1);
        rongqi1.addChild(fenxiang_mask);
        rongqi1.addChild(fenxinag);
        rongqi1.anchorOffsetX  = 320;
        rongqi1.anchorOffsetY = 568;
        rongqi1.x  = 320;
        rongqi1.y = 568;
        rongqi1.touchEnabled = true;
        rongqi1.scaleX = 0;
        // egret.Tween.get(rongqi1).to({scaleX:1},500);
        var This=this;
        var rannum_reday = Math.floor(Math.random() * 8);       //初始位置为下标 0 到 7
        cj_btn.addEventListener(egret.TouchEvent.TOUCH_TAP, function () {
            cj_btn.touchEnabled=false;
            cj_btn=my_button("抽奖\n    中    ", 55, 0xff4c22, true, 180, 180, 230, 563, "luck_cj_button_jpg", 9, 0xfff530);
            This.addChild(cj_btn);
            This.setChildIndex(cj_btn,3);
            egret.Tween.get(medal_10).to({}, 500).to({ visible: false }, 0);
            egret.Tween.get(text_11).to({}, 500).to({ visible: false }, 0);
            // medal_10.visible= false;
            // text_11.visible = false;
           

            var num = rannum_reday;                     //初始转动位置
            var j = 0;                                  //将转动的圈数声明一个变量
            var time = 500;                             //初始速度500毫秒  
            var rannum = Math.floor(Math.random() * 8); //到达位置下标值
            zhuanpan(rannum);
            var ding_content = this;
            function zhuanpan(x) {
                setTimeout(function () {
                    ding_content.ding.play(0, 1);
                    num--;
                    if (num < 0) {
                        num = 7;                        //从数组下标值0走到7一圈之后重置
                        j++;                            //进行加圈计算
                    }
                    if (j < 7) {                        //判断加速
                        time *= .9;
                        if (time < 100) {
                            time = 100;
                        }
                    } else {                            //减速
                        time *= 1.1;
                        if (time > 500) {
                            time = 500;
                        }
                    }
                    for (var i = 0; i < arr.length; i++) {
                        arr[i].visible = false;         //将第一个隐藏。。。
                        arr1[i].visible = false;
                        arr2[i].visible = false;
                    }
                    arr[num].visible = true;            //将第二个显示。。。
                    arr1[num].visible = true;           //显示奖牌上的礼物
                    arr2[num].visible = true;           //显示奖牌旁的字体

                    if (time == 500 && num == x) {//停止条件
                        egret.Tween.get(arr[num]).to({ scaleX: 1.2, scaleY: 1.2 }, 1000).to({ scaleX: 1, scaleY: 1 }, 1000).call(function () {
                        }, this);
                        if (num == 3) {
                            ding_content.defeated.play(0, 1);
                            //  text_10.visible = false;
                        } else {
                            ding_content.win.play(0, 1);
                            text_10.visible = true;
                            egret.Tween.get(text_10).to({ visible: false }, 200).to({ visible: true }, 200).to({ visible: false }, 200).to({ visible: true }, 200).to({ visible: false }, 200).to({ visible: true }, 200).call(function(){
                                cj_btn=my_button("再次\n抽奖", 55, 0xff4c22, true, 180, 180, 230, 563, "luck_cj_button_jpg", 9, 0xfff530);
                                This.addChild(cj_btn);
                                This.setChildIndex(cj_btn,4);
                            });

                        }
                        egret.Tween.get(rongqi1).to({scaleX:0},3000).to({scaleX:1},500);
                        rongqi1.addEventListener(egret.TouchEvent.TOUCH_TAP,function(){
                            egret.Tween.get(rongqi1).to({scaleX:0},500);
                        },this);

                        return;

                    }
                    zhuanpan(x);
                }, time)
            };
        }, this)
        // cj_btn.addEventListener(egret.TouchEvent.TOUCH_TAP, function () {
        //     alert("请完成指定条件再来抽奖");
        // }, this);

    }
}
